CloudWatch Logs Insights で Lambda のログで検出されるフィールドの値を確認してみた

CloudWatch Logs Insights で Lambda のログで検出されるフィールドの値を確認してみた

CloudWatch Logs Insightsで知らないフィールドがあったので、内容を確認してみました。
Clock Icon2024.12.20

CloudWatch Logs Insightsでは、標準的なフィールドに加えて、各サービスに合わせたフィールドも利用できます。

本記事では、Lambdaのログで検出されるフィールドの値がどうなっているかを確認してみました。

  • @timestamp
  • @logStream
  • @log
  • @message
  • @requestId
  • @duration
  • @billedDuration
  • @type
  • @memorySize
  • @maxMemoryUsed

なお、X-RayのトレースIDがログに含まれている場合は、下記も利用できます。本記事では省略します。

  • @xrayTraceId
  • @xraySegmentId

おすすめの方

  • CloudWatch Logs Insights で Lambda のログで検出されるフィールドの値を知りたい方

適当なLambdaを用意し、実行しておく

  • 関数名: manulaly-test-function
  • メモリ: 128 MB
def lambda_handler(event, context):
    print('botocore vertion is {0}'.format(botocore.__version__))
    print('boto3 vertion is {0}'.format(boto3.__version__))
    print('hello')

CloudWatch Logs Insights でログを確認する

CloudWatch Logsの内容

01_cloudwatch_logs

クエリ

fields @timestamp, @message, @logStream, @log, @requestId, @duration, @billedDuration, @type, @memorySize, @maxMemoryUsed
| sort @timestamp asc
| limit 10

クエリの実行結果(JSON)

[
    {
        "@timestamp": "2024-12-19 09:07:09.842",
        "@message": "INIT_START Runtime Version: python:3.7.v63\tRuntime Version ARN: arn:aws:lambda:ap-northeast-1::runtime:0452c013930b7a2594d5a8c146cc40d115ed83e2398bb66f3049298c64c7a327\n",
        "@logStream": "2024/12/19/[$LATEST]b196eecb1aa640edbf655ec5cd35d295",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "",
        "@duration": "",
        "@billedDuration": "",
        "@type": "",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:07:10.293",
        "@message": "START RequestId: 62f15eee-b3bc-468a-a4a3-72d7caafdc50 Version: $LATEST\n",
        "@logStream": "2024/12/19/[$LATEST]b196eecb1aa640edbf655ec5cd35d295",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "62f15eee-b3bc-468a-a4a3-72d7caafdc50",
        "@duration": "",
        "@billedDuration": "",
        "@type": "START",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:07:10.294",
        "@message": "botocore vertion is 1.29.90\n",
        "@logStream": "2024/12/19/[$LATEST]b196eecb1aa640edbf655ec5cd35d295",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "",
        "@duration": "",
        "@billedDuration": "",
        "@type": "",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:07:10.294",
        "@message": "boto3 vertion is 1.26.90\n",
        "@logStream": "2024/12/19/[$LATEST]b196eecb1aa640edbf655ec5cd35d295",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "",
        "@duration": "",
        "@billedDuration": "",
        "@type": "",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:07:10.294",
        "@message": "hello\n",
        "@logStream": "2024/12/19/[$LATEST]b196eecb1aa640edbf655ec5cd35d295",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "",
        "@duration": "",
        "@billedDuration": "",
        "@type": "",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:07:10.310",
        "@message": "END RequestId: 62f15eee-b3bc-468a-a4a3-72d7caafdc50\n",
        "@logStream": "2024/12/19/[$LATEST]b196eecb1aa640edbf655ec5cd35d295",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "62f15eee-b3bc-468a-a4a3-72d7caafdc50",
        "@duration": "",
        "@billedDuration": "",
        "@type": "END",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:07:10.310",
        "@message": "REPORT RequestId: 62f15eee-b3bc-468a-a4a3-72d7caafdc50\tDuration: 14.17 ms\tBilled Duration: 15 ms\tMemory Size: 128 MB\tMax Memory Used: 71 MB\tInit Duration: 450.55 ms\t\n",
        "@logStream": "2024/12/19/[$LATEST]b196eecb1aa640edbf655ec5cd35d295",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "62f15eee-b3bc-468a-a4a3-72d7caafdc50",
        "@duration": "14.17",
        "@billedDuration": "15",
        "@type": "REPORT",
        "@memorySize": "128000000",
        "@maxMemoryUsed": "71000000"
    }
]

ログフィールドの値を整理

名前 備考
timestamp 時刻(UTC)
message ログの内容
logStream ログストリーム名
log ロググループ名 +α
requestId リクエストID START,END,REPORTのみ
duration Duration (ms) REPORTのみ
billedDuration Billed Duration (ms) REPORT のみ
type START or END or REPORT START,END,REPORTのみ
memorySize Memory Size (Byte) REPORTのみ
maxMemoryUsed Max Memory Used (Byte) REPORTのみ

おまけ:LambdaのログフォーマットがJSONの場合

CloudWatch Logsの内容

02_cloudwatch_logs

クエリの実行結果(JSON)

「@message」の内容がJSONになっていますが、それ以外は同じです。

※一部、print()を利用してたせいで、JSONではないメッセージがあります。

[
    {
        "@timestamp": "2024-12-19 09:21:02.785",
        "@message": {
            "time": "2024-12-19T09:21:02.785Z",
            "type": "platform.initStart",
            "record": {
                "initializationType": "on-demand",
                "phase": "init",
                "runtimeVersion": "python:3.7.v63",
                "runtimeVersionArn": "arn:aws:lambda:ap-northeast-1::runtime:0452c013930b7a2594d5a8c146cc40d115ed83e2398bb66f3049298c64c7a327",
                "functionName": "manulaly-test-function",
                "functionVersion": "$LATEST"
            }
        },
        "@logStream": "2024/12/19/manulaly-test-function[$LATEST]dab4faee987c426bb21ba644bb0a97b6",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "",
        "@duration": "",
        "@billedDuration": "",
        "@type": "",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:21:03.236",
        "@message": {
            "time": "2024-12-19T09:21:03.236Z",
            "type": "platform.start",
            "record": {
                "requestId": "b12aeec0-cbd2-420c-a0f6-5753d5b3fd6e",
                "version": "$LATEST"
            }
        },
        "@logStream": "2024/12/19/manulaly-test-function[$LATEST]dab4faee987c426bb21ba644bb0a97b6",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "b12aeec0-cbd2-420c-a0f6-5753d5b3fd6e",
        "@duration": "",
        "@billedDuration": "",
        "@type": "START",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:21:03.237",
        "@message": "botocore vertion is 1.29.90\n",
        "@logStream": "2024/12/19/manulaly-test-function[$LATEST]dab4faee987c426bb21ba644bb0a97b6",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "",
        "@duration": "",
        "@billedDuration": "",
        "@type": "",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:21:03.237",
        "@message": "boto3 vertion is 1.26.90\n",
        "@logStream": "2024/12/19/manulaly-test-function[$LATEST]dab4faee987c426bb21ba644bb0a97b6",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "",
        "@duration": "",
        "@billedDuration": "",
        "@type": "",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:21:03.237",
        "@message": "hello\n",
        "@logStream": "2024/12/19/manulaly-test-function[$LATEST]dab4faee987c426bb21ba644bb0a97b6",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "",
        "@duration": "",
        "@billedDuration": "",
        "@type": "",
        "@memorySize": "",
        "@maxMemoryUsed": ""
    },
    {
        "@timestamp": "2024-12-19 09:21:03.259",
        "@message": {
            "time": "2024-12-19T09:21:03.259Z",
            "type": "platform.report",
            "record": {
                "requestId": "b12aeec0-cbd2-420c-a0f6-5753d5b3fd6e",
                "metrics": {
                    "durationMs": 20.421,
                    "billedDurationMs": 21,
                    "memorySizeMB": 128,
                    "maxMemoryUsedMB": 71,
                    "initDurationMs": 449.447
                },
                "status": "success"
            }
        },
        "@logStream": "2024/12/19/manulaly-test-function[$LATEST]dab4faee987c426bb21ba644bb0a97b6",
        "@log": "123456789123:/aws/lambda/manulaly-test-function",
        "@requestId": "b12aeec0-cbd2-420c-a0f6-5753d5b3fd6e",
        "@duration": "20.421",
        "@billedDuration": "21",
        "@type": "REPORT",
        "@memorySize": "128000000",
        "@maxMemoryUsed": "71000000"
    }
]

さいごに

REPORT行のみ調べたい場合などは、「@type」を使えば便利そうです。

参考

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.